package com.fitns.user.service.baseConfig;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.fitns.user.conf.Constants;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.dao.baseConfig.TbVehicleLederDao;
import com.fitns.user.dto.baseConfig.TbVehicleLedgerDTO;
import com.fitns.user.dto.baseConfig.TbVehicleLedgerVO;
import com.fitns.user.model.baseConfig.TbVehicleLedger;
import com.fitns.user.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;

@Service
public class TbVehicleLederService {

	@Autowired
	private TbVehicleLederDao tbVehicleLederDao;

	@Transactional
	public int insertVehicleLeder(TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		TbVehicleLedger record = new TbVehicleLedger();
		BeanUtils.copyProperties(tbVehicleLedgerDTO, record);
		record.setInputTime(new Date());
		return tbVehicleLederDao.insertSelective(record);

	}

	@Transactional
	public int updateVehicleLeder(TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		TbVehicleLedger record = new TbVehicleLedger();
		BeanUtils.copyProperties(tbVehicleLedgerDTO, record);
		record.setId(tbVehicleLedgerDTO.getId());
		record.setInputTime(new Date());
		return tbVehicleLederDao.updateByPrimaryKeySelective(record);
	}

	@Transactional
	public int deleteVehicleLeder(long id) {
		return tbVehicleLederDao.deleteVehicleLeder(id);
	}

	/** 查看车辆台账 */
	public PageInfo<TbVehicleLedgerVO> selectVehicleLedger(TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		tbVehicleLedgerDTO.pageHandle();
		List<TbVehicleLedgerVO> select = tbVehicleLederDao.selectVehicleLedger(tbVehicleLedgerDTO);
		PageInfo<TbVehicleLedgerVO> selectVehicle = new PageInfo<>(select);
		return selectVehicle;
	}

	public TbVehicleLedger selectVehicleLederById(long id) {
		return tbVehicleLederDao.selectVehicleLederById(id);
	}

	@Transactional
	public Integer inserImage(TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		TbVehicleLedger record = new TbVehicleLedger();
		BeanUtils.copyProperties(tbVehicleLedgerDTO, record);
		if (Validator.isNull(tbVehicleLedgerDTO.getId())) {

			return this.insertVehicleLeder(tbVehicleLedgerDTO);
		} else {
			record.setImage(tbVehicleLedgerDTO.getImage());
			record.setModifyTime(new Date());
			return tbVehicleLederDao.updateByPrimaryKey(record);
		}

	}

	@Transactional
	public List<TbVehicleLedgerVO> importExcel(List<List<String>> excelList) {
		List<TbVehicleLedgerVO> dtoList = new ArrayList<>();
		List<TbVehicleLedgerVO> msgList = new ArrayList<>();

		for (int i = 1; i < excelList.size(); i++) {
			String number = excelList.get(i).get(0);
			if (!StringUtils.isEmpty(number)) {
				TbVehicleLedgerVO facilityDataVo = this.validateExcel(excelList.get(i), i);
				if (!StringUtils.isEmpty(facilityDataVo.getMsg())) {
					msgList.add(facilityDataVo);
				} else {
					TbVehicleLedgerVO dataVo = this.importDetail(excelList.get(i));
					dtoList.add(dataVo);
				}
			}
		}
		if (Validator.isEmpty(msgList) || msgList.size() == 0) {
			if (Validator.isNotEmpty(dtoList) && dtoList.size() != 0) {
				dtoList.forEach(facilityDataVo -> {
					TbVehicleLedger facilityData = new TbVehicleLedger();
					BeanUtils.copyProperties(facilityDataVo, facilityData);
					tbVehicleLederDao.insertSelective(facilityData);
				});
			} else {
				TbVehicleLedgerVO facilityDataVo = new TbVehicleLedgerVO();
				facilityDataVo.setMsg(ExcelConstants.VEHICLE_LEDGER_IMPORT.EXCEL_IS_NULL);
				msgList.add(facilityDataVo);
			}
		}

		return msgList;

	}

	/** excel文件导入字段必填字段 */
	public TbVehicleLedgerVO validateExcel(List<String> excelList, int i) {
		TbVehicleLedgerVO facilityDataVo = new TbVehicleLedgerVO();
		StringBuilder msg = new StringBuilder();

		if (StringUtils.isEmpty(excelList.get(0))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_PLATE_NUM_0);
		}
		if (StringUtils.isEmpty(excelList.get(1))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_DRIVER_NAME_1);
		}
		if (StringUtils.isEmpty(excelList.get(2))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_DRIVER_PHONE_2);
		}
		if (StringUtils.isEmpty(excelList.get(3))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_TYPE_3);
		}
		if (StringUtils.isEmpty(excelList.get(4))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_TERMINAL_4);
		}
		if (StringUtils.isEmpty(excelList.get(5))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_STATUS_5);
		}
		if (StringUtils.isEmpty(excelList.get(6))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_LET_STANDARD_6);
		}
		if (StringUtils.isEmpty(excelList.get(7))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_FUEL_TYPE_7);
		}
		if (StringUtils.isEmpty(excelList.get(8))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_PRODUCT_DATE_8);
		}
		if (StringUtils.isEmpty(excelList.get(9))) {
			msg.append("第" + (i + 1) + "行" + ExcelConstants.VEHICLE_LEDGER_IMPORT.VEHICLE_LEDGER_DURABLE_YEARS_9);
		}

		if (!StringUtils.isEmpty(msg)) {
			facilityDataVo.setMsg(msg.toString());
		}
		return facilityDataVo;
	}

	public TbVehicleLedgerVO importDetail(List<String> excelList) {
		TbVehicleLedgerVO facilityDataVo = new TbVehicleLedgerVO();
		// 不为string类型的if转换一下类型
		facilityDataVo.setPlateNum(excelList.get(0));
		facilityDataVo.setDriverName(excelList.get(1));
		facilityDataVo.setDriverPhone(excelList.get(2));
		facilityDataVo.setType(excelList.get(3));
		facilityDataVo.setTerminal(excelList.get(4));
		if (Validator.isNotNull(excelList.get(5))) {
			facilityDataVo.setStatus(Integer.valueOf(excelList.get(5)));
		}
		facilityDataVo.setLetStandard(excelList.get(6));
		facilityDataVo.setFuelType(excelList.get(7));
		if (Validator.isNotNull(excelList.get(8))) {
			facilityDataVo.setProductDate(DateUtil.parseStrToDate(excelList.get(8), Constants.DATESTR));
		}
		if (Validator.isNotNull(excelList.get(9))) {
			facilityDataVo.setDurableYears(Integer.valueOf(excelList.get(9)));
		}
		facilityDataVo.setRanges(excelList.get(10));
		facilityDataVo.setProperty(excelList.get(11));
		facilityDataVo.setPurpose(excelList.get(12));
		facilityDataVo.setTransType(excelList.get(13));
		if (Validator.isNotNull(excelList.get(14))) {
			facilityDataVo.setBuyDate(DateUtil.parseStrToDate(excelList.get(14), Constants.DATESTR));
		}
		if (Validator.isNotNull(excelList.get(15))) {
			facilityDataVo.setInstallDate(DateUtil.parseStrToDate(excelList.get(15), Constants.DATESTR));
		}
		if (Validator.isNotNull(excelList.get(16)) && !"".equals(excelList.get(16))) {
			facilityDataVo.setValidePeriod(Integer.valueOf(excelList.get(16)));
		}
		if (Validator.isNotNull(excelList.get(17))) {
			facilityDataVo.setTranship(Boolean.valueOf(excelList.get(17)));
		}

		facilityDataVo.setRoadCertificate(excelList.get(18));
		facilityDataVo.setBrandModel(excelList.get(19));
		facilityDataVo.setChangeCom(excelList.get(20));
		facilityDataVo.setEngineNum(excelList.get(21));
		if (Validator.isNotNull(excelList.get(22)) && !"".equals(excelList.get(22))) {
			facilityDataVo.setApprovedNum(Integer.valueOf(excelList.get(22)));
		}
		if (Validator.isNotNull(excelList.get(23)) && !"".equals(excelList.get(23))) {
			facilityDataVo.setApprovedWeight(Float.valueOf(excelList.get(23)));
		}
		facilityDataVo.setVehiclePower(excelList.get(24));
		facilityDataVo.setFrameNum(excelList.get(25));
		facilityDataVo.setUnderpanType(excelList.get(26));
		if (Validator.isNotNull(excelList.get(27)) && !"".equals(excelList.get(27))) {
			facilityDataVo.setDeadWeight(Float.valueOf(excelList.get(27)));
		}
		if (Validator.isNotNull(excelList.get(28)) && !"".equals(excelList.get(27))) {
			facilityDataVo.setLength(Float.valueOf(excelList.get(28)));
		}
		if (Validator.isNotNull(excelList.get(29)) && !"".equals(excelList.get(29))) {
			facilityDataVo.setWidth(Float.valueOf(excelList.get(29)));
		}
		if (Validator.isNotNull(excelList.get(30)) && !"".equals(excelList.get(30))) {
			facilityDataVo.setHeight(Float.valueOf(excelList.get(30)));
		}
		if (Validator.isNotNull(excelList.get(31)) && !"".equals(excelList.get(31))) {
			facilityDataVo.setOilWear(Float.valueOf(excelList.get(31)));
		}
		if (Validator.isNotNull(excelList.get(32)) && !"".equals(excelList.get(32))) {
			facilityDataVo.setSpeedLimit(Float.valueOf(excelList.get(32)));
		}
		if (Validator.isNotNull(excelList.get(33)) && !"".equals(excelList.get(33))) {
			facilityDataVo.setFeatureNum(Float.valueOf(excelList.get(33)));
		}
		if (Validator.isNotNull(excelList.get(34)) && !"".equals(excelList.get(34))) {
			facilityDataVo.setPolePairs(Integer.valueOf(excelList.get(34)));
		}
		if (Validator.isNotNull(excelList.get(35)) && !"".equals(excelList.get(35))) {
			facilityDataVo.setInputVoltage(Float.valueOf(excelList.get(35)));
		}
		if (Validator.isNotNull(excelList.get(36)) && !"".equals(excelList.get(36))) {
			facilityDataVo.setOneParam(Float.valueOf(excelList.get(36)));
		}
		if (Validator.isNotNull(excelList.get(37)) && !"".equals(excelList.get(37))) {
			facilityDataVo.setTwoParam(Float.valueOf(excelList.get(37)));
		}
		facilityDataVo.setIllegalMove(excelList.get(38));
		facilityDataVo.setLineAlarm(excelList.get(39));

		if (Validator.isNotNull(excelList.get(40))) {
			facilityDataVo.setCamera(Boolean.valueOf(excelList.get(40)));
		}
		if (Validator.isNotNull(excelList.get(41))) {
			facilityDataVo.setVideo(Boolean.valueOf(excelList.get(41)));
		}
		facilityDataVo.setVideoNum(excelList.get(42));
		facilityDataVo.setStopArea(excelList.get(43));
		if (Validator.isNotNull(excelList.get(44)) && !"".equals(excelList.get(44))) {
			facilityDataVo.setStopTime(Integer.valueOf(excelList.get(44)));
		}
		if (Validator.isNotNull(excelList.get(45)) && !"".equals(excelList.get(45))) {
			facilityDataVo.setWorkRation(Integer.valueOf(excelList.get(45)));
		}
		facilityDataVo.setRecorderNum(excelList.get(46));
		facilityDataVo.setVideoAddress(excelList.get(47));
		facilityDataVo.setImage(excelList.get(48));
		return facilityDataVo;
	}

	public List<List<String>> export(TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		List<TbVehicleLedger> facilityDataList = tbVehicleLederDao.searchForExport(tbVehicleLedgerDTO);
		List<List<String>> excelList = new ArrayList<>();
		for (int i = 0; i < facilityDataList.size(); i++) {
			if (i == 0) {
				excelList.add(this.getRowHeader(ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.class));
			}
			excelList.add(this.getrow(facilityDataList.get(i), i));
		}
		return excelList;
	}

	public List<String> getRowHeader(Class clazz) {
		Field[] fields = clazz.getDeclaredFields();
		List<String> rowHeader = new ArrayList<>();
		for (int i = 0; i < fields.length - 1; i++) {
			try {
				Object object = fields[i].get(clazz);
				String value = object.toString();
				rowHeader.add(value);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return rowHeader;
	}

	public List<String> getrow(TbVehicleLedger tbVehicleLedger, int i) {
		List<String> row = new ArrayList<>();

		//row.add(String.valueOf(i + 1));
		row.add(tbVehicleLedger.getPlateNum());
		row.add(tbVehicleLedger.getDriverName());
		row.add(tbVehicleLedger.getDriverPhone());
		row.add(tbVehicleLedger.getType());
		row.add(tbVehicleLedger.getTerminal());
		if (Validator.isNull(tbVehicleLedger.getStatus())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getStatus()));
		}

		row.add(tbVehicleLedger.getLetStandard());
		row.add(tbVehicleLedger.getFuelType());

		if (Validator.isNull(tbVehicleLedger.getProductDate())) {
			row.add("");
		} else {
			row.add(DateUtil.parseDateToStr(tbVehicleLedger.getProductDate(), Constants.DATESTR));
		}
		if (Validator.isNull(tbVehicleLedger.getDurableYears())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getDurableYears()));
		}
		row.add(tbVehicleLedger.getRanges());
		row.add(tbVehicleLedger.getProperty());
		row.add(tbVehicleLedger.getPurpose());
		row.add(tbVehicleLedger.getTransType());

		if (Validator.isNull(tbVehicleLedger.getBuyDate())) {
			row.add("");
		} else {
			row.add(DateUtil.parseDateToStr(tbVehicleLedger.getBuyDate(), Constants.DATESTR));
		}

		if (Validator.isNull(tbVehicleLedger.getInstallDate())) {
			row.add("");
		} else {
			row.add(DateUtil.parseDateToStr(tbVehicleLedger.getInstallDate(), Constants.DATESTR));
		}
		if (Validator.isNull(tbVehicleLedger.getValidePeriod())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getValidePeriod()));
		}
		if (Validator.isNull(tbVehicleLedger.getTranship())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getTranship()));
		}
		row.add(tbVehicleLedger.getRoadCertificate());
		row.add(tbVehicleLedger.getBrandModel());
		row.add(tbVehicleLedger.getChangeCom());
		row.add(tbVehicleLedger.getEngineNum());

		if (Validator.isNull(tbVehicleLedger.getApprovedNum())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getApprovedNum()));
		}
		if (Validator.isNull(tbVehicleLedger.getApprovedWeight())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getApprovedWeight()));
		}
		row.add(tbVehicleLedger.getVehiclePower());
		row.add(tbVehicleLedger.getFrameNum());
		row.add(tbVehicleLedger.getUnderpanType());

		if (Validator.isNull(tbVehicleLedger.getDeadWeight())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getDeadWeight()));
		}
		if (Validator.isNull(tbVehicleLedger.getLength())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getLength()));
		}
		if (Validator.isNull(tbVehicleLedger.getWidth())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getWidth()));
		}
		if (Validator.isNull(tbVehicleLedger.getHeight())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getHeight()));
		}
		if (Validator.isNull(tbVehicleLedger.getOilWear())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getOilWear()));
		}
		if (Validator.isNull(tbVehicleLedger.getSpeedLimit())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getSpeedLimit()));
		}
		if (Validator.isNull(tbVehicleLedger.getFeatureNum())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getFeatureNum()));
		}
		if (Validator.isNull(tbVehicleLedger.getPolePairs())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getPolePairs()));
		}

		if (Validator.isNull(tbVehicleLedger.getInputVoltage())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getInputVoltage()));
		}
		if (Validator.isNull(tbVehicleLedger.getOneParam())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getOneParam()));
		}
		if (Validator.isNull(tbVehicleLedger.getTwoParam())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getTwoParam()));
		}

		row.add(tbVehicleLedger.getIllegalMove());
		row.add(tbVehicleLedger.getLineAlarm());
		if (Validator.isNull(tbVehicleLedger.getCamera())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getCamera()));
		}
		if (Validator.isNull(tbVehicleLedger.getVideo())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getVideo()));
		}
		row.add(tbVehicleLedger.getVideoNum());
		row.add(tbVehicleLedger.getStopArea());
		if (Validator.isNull(tbVehicleLedger.getStopTime())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getStopTime()));
		}
		if (Validator.isNull(tbVehicleLedger.getWorkRation())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbVehicleLedger.getWorkRation()));
		}

		row.add(tbVehicleLedger.getRecorderNum());
		row.add(tbVehicleLedger.getVideoAddress());
		row.add(tbVehicleLedger.getImage());
		return row;
	}
}
